/**
 * 所有版权归 广西梧州 陈锦韬 所有
 *
 * @Title: UrlAnalyse
 * @Package com.rx.core.data
 * @Description: URL 分析工具
 * @author: 陈锦韬
 * @date: 2021\6\9 0009
 * @version V1.0
 * @Copyright: 2021 陈锦韬  All rights reserved.
 */
package com.rx.core.data;

import com.rx.core.base.RxBaseConstant;
import com.rx.core.dbase.RxBaseMapper;
import com.rx.core.util.EmptyChecker;
import com.rx.core.util.RxUtil;
import com.rx.core.util.SpringUtil;
import lombok.Data;

/**
 * @author: Administrator
 * @Description: URL 分析工具
 * @date: 2021\6\9 0009
 */
@Data
public class UrlAnalyse {
    public static final String DAO_SUFFIX = "Dao";
    public static final String MAPPER_SUFFIX = "Mapper";

    private static String FIRST_SUFFIX = DAO_SUFFIX;
    private static String SECOND_SUFFIX = MAPPER_SUFFIX;


    private  String url;
    private  String api;
    private  String type;
    private  String service;
    private  String form;
    private  String method;

    public String getHumpUrl(){
        return RxUtil.StringUtil.dashToHump(url);
    }
    public String getHumpApi(){
        return RxUtil.StringUtil.dashToHump(api);
    }
    public String getHumpType(){
        return RxUtil.StringUtil.dashToHump(type);
    }
    public String getHumpService(){
        return RxUtil.StringUtil.dashToHump(service);
    }
    public String getHumpForm(){
        return RxUtil.StringUtil.dashToHump(form);
    }
    public String getHumpMethod(){
        return RxUtil.StringUtil.dashToHump(method);
    }
    private static String getUrlOffset(String[] uris ,int offset){
        if (uris.length > offset){
            return uris[offset].toLowerCase();
        }
        return null;
    }

    /**
     * 創建url信息
     * @param uri
     * @return
     */
    public static UrlAnalyse create(String uri){
        if (EmptyChecker.isEmpty(uri)){
            return new UrlAnalyse();
        }
        String[] uris = uri.split("/");

        UrlAnalyse urlAnalyse = new UrlAnalyse();
        urlAnalyse.url = uri;
        urlAnalyse.setApi(getUrlOffset(uris,RxBaseConstant.URL_OFFSET_API));
        urlAnalyse.setType(getUrlOffset(uris,RxBaseConstant.URL_OFFSET_TYPE));
        urlAnalyse.setForm(getUrlOffset(uris,RxBaseConstant.URL_OFFSET_FORM));
        urlAnalyse.setService(getUrlOffset(uris,RxBaseConstant.URL_OFFSET_SERVICE));
        urlAnalyse.setMethod(getUrlOffset(uris,RxBaseConstant.URL_OFFSET_METHOD));
        return urlAnalyse;
    }

    /**
     * 横杠转驼峰，且加上后缀
     * @param name
     * @param suffix
     * @return
     */
    public static String getHumpWithSuffix(String name,String suffix){
        if(name.contains(suffix)){
            return RxUtil.StringUtil.dashToHump(name);
        }
        StringBuilder sb = new StringBuilder(RxUtil.StringUtil.dashToHump(name));
        sb.append(suffix);
        return sb.toString();
    }

    /**
     * 以哪个后缀名先查询，减少异常
     * @param firstSuffix
     * @param secondSuffix
     * @return
     */
    private RxBaseMapper getMapperBySuffix(String firstSuffix,String secondSuffix){
        RxBaseMapper mapper = null;
        try{
            mapper = (RxBaseMapper) SpringUtil.getBean(getHumpWithSuffix(getHumpForm(),firstSuffix));
            FIRST_SUFFIX = firstSuffix;
            SECOND_SUFFIX = secondSuffix;
        }catch (Throwable e){
            mapper = (RxBaseMapper) SpringUtil.getBean(getHumpWithSuffix(getHumpForm(),secondSuffix));
            FIRST_SUFFIX = secondSuffix;
            SECOND_SUFFIX = firstSuffix;
        }
        return mapper;
    }


    public RxBaseMapper getMapper(){
        return getMapperBySuffix(FIRST_SUFFIX,SECOND_SUFFIX);
    }
}
